import 'th_credentials.dart';
import 'th_credentials_manager_platform_interface.dart';
import 'th_result.dart';

abstract class ThCredentialsManager {
  static Future<String?> getPlatformVersion() {
    return ThCredentialsManagerPlatform.instance.getPlatformVersion();
  }

  static Future<THResult<THCredentials?>> getCredentialsBy(
      String borderAgentID) async {
    THResult<List<THCredentials>> res =
        await ThCredentialsManagerPlatform.instance.getAllCredentials();
    THCredentials? need;
    THResult<THCredentials?> result = THResult();
    if (res.success) {
      res.data!.forEach(
        (e) {
          if (e.borderAgentId == borderAgentID) need = e;
        },
      );
    } else {
      result.success = res.success;
      result.msg = res.msg;
    }
    result.data = need;

    return result;
  }

  static Future<THResult<List<THCredentials>>> getAllCredentials() {
    return ThCredentialsManagerPlatform.instance.getAllCredentials();
  }

  static Future<THResult<bool>> deleteCredentialsBy(
      String borderAgentID) async {
    return ThCredentialsManagerPlatform.instance.deleteCredentialsBy(
      borderAgentID,
    );
  }

  static Future<THResult<bool>> storeCredentials({
    required String borderAgentID,
    required String activeDataSet,
  }) async {
    return ThCredentialsManagerPlatform.instance.storeCredentials(
      borderAgentID: borderAgentID,
      activeDataSet: activeDataSet,
    );
  }

  static Future<THResult<bool>> isPreferred({
    required String activeDataSet,
  }) async {
    return ThCredentialsManagerPlatform.instance.isPreferred(
      activeDataSet: activeDataSet,
    );
  }
}
